/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tk.eigh.ksvg.types;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author nt
 */
public class SVGLengthEditor implements PropertyEditor {

    protected final static Pattern PATTERN = Pattern.compile(
            "(".concat(SVGNumber.NUMBER_PATTERN_STRING).concat(")\\s*").concat(SVGUnits.UNITS_PATTERN_STRING).concat("?"));
    SVGLength _length;

    public void setValue(Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getValue() {
        return this._length;
    }

    public boolean isPaintable() {
        return false;
    }

    public void paintValue(Graphics gfx, Rectangle box) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getJavaInitializationString() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getAsText() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setAsText(String text) throws IllegalArgumentException {
        String trimmedValue = text.trim();
        Matcher matcher = PATTERN.matcher(trimmedValue);
        if (matcher.matches()) {
            SVGNumber number = new SVGNumber(matcher.group(1));
            if (matcher.group(2) != null) {
                this._length = new SVGLength(number, SVGUnits.valueOf(matcher.group(2).replace("%", "percent")));
            } else {
                this._length = new SVGLength(number);
            }
        } else {
            if (trimmedValue.equalsIgnoreCase("inherit")) {
                this._length = new SVGLength();
            } else {
                throw new IllegalArgumentException(SVGLength.class.getName().concat(" must match pattern ").concat(PATTERN.pattern()));
            }
        }
    }

    public String[] getTags() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Component getCustomEditor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean supportsCustomEditor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
